from scrapy.spiders import Spider
from config import URL_SEEDS
from ..items import FronteraDemoItem
from scrapy.exceptions import DontCloseSpider
from scrapy.signals import spider_idle


class GamespiderSpider(Spider):
    name = 'gamespider'
    start_urls = URL_SEEDS

    @classmethod
    def from_crawler(cls, crawler, *args, **kwargs):
        spider = cls(*args, **kwargs)
        spider._set_crawler(crawler)
        spider.crawler.signals.connect(spider.spider_idle, signal=spider_idle)
        return spider

    def spider_idle(self):
        self.log("Spider idle signal caught.")
        raise DontCloseSpider

    def __init__(self, *args, **kwargs):
        super(GamespiderSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        item = FronteraDemoItem()
        games = response.xpath('//div[@class="ztliswrap"]/div')
        for game in games:
            item['name'] = game.xpath('.//a[2]//text()').extract_first("").strip()
            item['rating'] = game.xpath('.//font//text()').extract_first("")
            yield item
